/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.objetos;

import java.sql.*;

/**
 *
 * @author dskaster
 */
public class Conexao 
{  
    private Connection con;
    private Statement stm;
    private String driver;
    private String url;
    private String usuario;
    private String senha;
    public void inicia(){
		url = "jdbc:postgresql://localhost:5432/bars";
		usuario = "postgres";
		senha ="batata";
		driver = "org.postgresql.Driver";
				
		try{
			Class.forName(driver);
			con = DriverManager.getConnection(url, usuario, senha);
			stm = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
                       
                }
		catch(ClassNotFoundException e)
		{
			System.out.println("Driver não encontrado...\n");
			e.printStackTrace();
		}
		catch(SQLException e)
		{
			System.out.println("Nao foi possivel se conectar ao banco...\n");
			e.printStackTrace();
		}
	}
	public ResultSet executa(String query) throws SQLException{
	    ResultSet res = stm.executeQuery(query);
		return res;
	}
	
	public void atualiza(String query)throws SQLException{
	   stm.executeUpdate(query);
	
	}
	public void encerra() throws SQLException{
		try
          {
              stm.close();
              con.close();
          }
          catch(Exception e)
          {
              System.out.println("Não foi possivel fechar a conexao...");
              e.printStackTrace();
          }
	}
		  
}